package com.me.batm.screens;

import java.util.Vector;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.me.batm.GameConstants;
import com.me.batm.OurGdxGame;
import com.me.batm.input.InputStatus;

public class DisplayImageScreen implements Screen{

	OurGdxGame ourGdxGame;
	Texture splsh;
	SpriteBatch spriteBatch;
	long timerInit;
    long duration;
    Vector<String> fileImagesVector;
    int imageIterator;
    String fileImage;
    int numClass;
    ShapeRenderer shapeRenderer;
    boolean initClass = false;
	InputStatus inputStatus;

    public DisplayImageScreen(OurGdxGame ourGdxGame, int numClass, String fileImage, long duration) {
    	this.ourGdxGame = ourGdxGame;
    	this.timerInit = System.currentTimeMillis();
    	this.duration = duration;
    	this.fileImage = fileImage;
    	this.numClass = numClass;
    	fileImagesVector = null;
    }
    
    public DisplayImageScreen(OurGdxGame ourGdxGame, int numClass, Vector<String> imageVector, long duration) {
    	this.ourGdxGame = ourGdxGame;
    	timerInit = System.currentTimeMillis();
    	this.duration = duration;
    	this.fileImagesVector = imageVector;
    	this.numClass = numClass;
    	imageIterator = 0;
    	shapeRenderer = new ShapeRenderer();
    	initClass = true;
    }
    
    
	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
         spriteBatch.begin();
         spriteBatch.draw(splsh, 0, 
        		 				 0,
        		 				 GameConstants.DESKTOP_SCREEN_WIDTH,GameConstants.DESKTOP_SCREEN_HEIGHT);
         spriteBatch.end();
         
         float _delta = delta/3;
         float alpha  = delta*2f;
         //System.out.println(delta*30f);
         Gdx.gl.glEnable(GL10.GL_BLEND);
         shapeRenderer.begin(ShapeType.FilledRectangle);
         shapeRenderer.setColor(0f, 0f, 0f, alpha);
         shapeRenderer.filledRect(0, 0, GameConstants.DESKTOP_SCREEN_WIDTH, GameConstants.DESKTOP_SCREEN_HEIGHT);
         shapeRenderer.end();
         Gdx.gl.glDisable(GL10.GL_BLEND);
         rotateImage();
         
	}

	public void rotateImage() {		
		if(Gdx.input.isKeyPressed(Keys.ESCAPE)) {
			System.out.println("Space");
			ourGdxGame.setScreen(new MenuScreen(DisplayImageScreen.this.ourGdxGame));			
		}
		
		if(System.currentTimeMillis() - timerInit > duration || Gdx.input.isKeyPressed(Keys.RIGHT)) {        	 
        	if(fileImagesVector != null && imageIterator < fileImagesVector.size()) {
        		spriteBatch = new SpriteBatch();
        		splsh = new Texture(Gdx.files.internal(fileImagesVector.get(imageIterator++)));
            	timerInit = System.currentTimeMillis();
            }else {
	        	switch (numClass) {
					case 1:
						ourGdxGame.setScreen(new MenuScreen(DisplayImageScreen.this.ourGdxGame));
						break;
					default:
						break;
				}        	 
            }
         }
	}
	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		spriteBatch = new SpriteBatch();
		inputStatus = new InputStatus();

		if(fileImagesVector != null && fileImagesVector.size() > 0)
			splsh = new Texture(Gdx.files.internal(fileImagesVector.get(imageIterator++)));
		else
			splsh = new Texture(Gdx.files.internal(fileImage));		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
